var t = getApp();

Page({
    data: {
        confirm: !1,
        height: 300,
        pathArr: [],
        picPath: "",
        title: "",
        starList: [ {
            text: "非常差",
            starBoo: !1
        }, {
            text: "差",
            starBoo: !1
        }, {
            text: "一般",
            starBoo: !1
        }, {
            text: "好",
            starBoo: !1
        }, {
            text: "非常好",
            starBoo: !1
        } ],
        starText: "",
        chooseImageBoo: !1
    },
    onLoad: function(a) {
        var e = a.orderId, o = a.current || "", i = a.target || "", n = wx.getStorageSync("logs") || "", s = wx.getStorageSync("orderList"), r = {}, c = "", d = "", h = "", u = "";
        1 == o ? r = s.orderItems : s.forEach(function(t) {
            t.orderId != e || (r = t.orderItems);
        }), r.forEach(function(t) {
            if (0 == t.buyerRate) return d = t.num, c = t.itemId, h = t.title, void (u = t.picPath);
        }), n || wx.login({
            success: function(a) {
                var e = a.code;
                wx.request({
                    url: t.globalData.httpLogin + "user/code",
                    data: {
                        code: e,
                        token: n
                    },
                    success: function(t) {
                        var a = t.data.data.token, e = t.data.data.bindPhone;
                        try {
                            wx.setStorageSync("logs", a);
                        } catch (t) {}
                        try {
                            wx.setStorageSync("bindPhone", e);
                        } catch (t) {}
                    },
                    fail: function(t) {}
                });
            }
        }), this.setData({
            token: n,
            orderId: e,
            itemId: c,
            itemParam: d,
            title: h,
            picPath: u,
            target: i
        });
    },
    bindlinechange: function(t) {
        t.detail.heightRpx > 300 ? this.setData({
            height: t.detail.heightRpx
        }) : this.setData({
            height: 300
        });
    },
    chooseImage: function() {
        var t = this, a = this.data.pathArr;
        wx.chooseImage({
            count: 9,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                for (var o = e.tempFilePaths, i = 0; i < o.length; i++) if (a.push(o[i]), a.length >= 9) {
                    t.setData({
                        chooseImageBoo: !0
                    });
                    break;
                }
                t.setData({
                    pathArr: a
                });
            }
        });
    },
    delImage: function(t) {
        var a = t.target.dataset.index, e = this.data.pathArr;
        e.splice(a, 1), e.length < 9 && this.setData({
            chooseImageBoo: !1
        }), this.setData({
            pathArr: e
        });
    },
    previewImage: function(t) {
        var a = t.target.dataset.imgindex, e = this.data.pathArr, o = e[a];
        wx.previewImage({
            current: o,
            urls: e,
            success: function(t) {}
        });
    },
    changeStar: function(t) {
        for (var a = t.target.dataset.current, e = this.data.starList, o = 0; o < e.length; o++) e[o].starBoo = o <= a;
        this.setData({
            starList: e,
            starText: e[a].text
        });
    },
    bindblur: function(t) {
        var a = t.detail.value;
        this.setData({
            estimate: a
        });
    },
    bindFormSubmit: function(a) {
        wx.showLoading({
            mask: !0
        });
        var e = this.data.starList, o = 0, i = this.data.orderId, n = this.data.itemId, s = this.data.itemParam, r = this.data.estimate, c = (this.data.token, 
        this.data.pathArr), d = c.length, h = t.globalData.httpOne + "mall/estimate/uploadEstimateImage";
        e.forEach(function(t) {
            1 == t.starBoo && o++;
        }), r ? o <= 0 ? wx.showToast({
            title: "请对商品进行评分",
            icon: "none",
            duration: 2e3
        }) : d <= 0 ? this.callback(i, n, r, o, s) : this.uploadDIY(c, 0, d, h, n, i, r, o, s) : wx.showToast({
            title: "请输入评论的内容~",
            icon: "none",
            duration: 2e3
        });
    },
    uploadDIY: function(a, e, o, i, n, s, r, c, d) {
        var h = this, u = this;
        wx.uploadFile({
            url: i,
            method: "POST",
            header: {
                "content-type": "multipart/form-data"
            },
            filePath: a[e],
            name: "file",
            formData: {
                token: u.data.token,
                itemId: n
            },
            success: function(l) {
                ++e == o ? wx.request({
                    url: t.globalData.httpOne + "mall/estimate/save",
                    method: "POST",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        token: u.data.token,
                        orderId: s,
                        itemId: n,
                        estimate: r,
                        star: c,
                        itemParam: d
                    },
                    success: function(t) {
                        wx.hideLoading(), 200 == t.data.code && wx.showToast({
                            title: "评价成功，感谢您的评价~",
                            icon: "none",
                            duration: 2e3,
                            success: function() {
                                setTimeout(function() {
                                    1 == u.data.target ? wx.navigateBack({
                                        delta: 1
                                    }) : wx.navigateBack({
                                        delta: 2
                                    });
                                }, 1e3);
                            }
                        });
                    },
                    fail: function() {
                        wx.showToast({
                            title: "系统繁忙，请稍后重试",
                            icon: "none",
                            duration: 2e3
                        });
                    },
                    complete: function() {}
                }) : h.uploadDIY(a, e, o, i, n, s, r, c, d);
            },
            fail: function(t) {
                wx.showToast({
                    title: "系统繁忙，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            },
            complete: function() {}
        });
    },
    callback: function(a, e, o, i, n) {
        var s = this;
        wx.request({
            url: t.globalData.httpOne + "mall/estimate/save",
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                token: s.data.token,
                orderId: a,
                itemId: e,
                estimate: o,
                star: i,
                itemParam: n
            },
            success: function(t) {
                200 == t.data.code && wx.showToast({
                    title: "评价成功，感谢您的评价~",
                    icon: "none",
                    duration: 2e3,
                    success: function() {
                        setTimeout(function() {
                            1 == s.data.target ? wx.navigateBack({
                                delta: 1
                            }) : wx.navigateBack({
                                delta: 2
                            });
                        }, 1e3);
                    }
                });
            },
            fail: function() {
                wx.showToast({
                    title: "系统繁忙，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            },
            complete: function() {}
        });
    }
});